let unIpt = document.querySelector('#unIpt');
let pwIpt = document.querySelector('#pwIpt');
let btn = document.querySelector('#btn');

let unIptFlage = false;
let pwIptFlage = false;

// 用户名聚焦

unIpt.onfocus = function () {
    console.log("用户框聚焦");
    this.nextElementSibling.innerHTML = '请输入您的用户名';
    this.nextElementSibling.style.color = 'rgb(219, 159, 29)';
};

// 用户名失焦
unIpt.onblur = function () {
    console.log("用户框失焦");
    let unIptVal = unIpt.value;
    let unIptRegular = /\w{5,17}$/;
    if (unIptRegular.test(unIptVal)) {
        this.nextElementSibling.innerHTML = '√';
        this.nextElementSibling.style.color = 'green';
        unIptFlage = true;
    } else {
        this.nextElementSibling.innerHTML = '！ 请检查您的用户名是否正确';
        this.nextElementSibling.style.color = 'red';
    }
};

// 密码框聚焦
pwIpt.onfocus = function () {
    console.log("密码框聚焦");
    this.nextElementSibling.innerHTML = '请输入您的密码';
    this.nextElementSibling.style.color = 'rgb(219, 159, 29)';
};

// 密码框失焦
pwIpt.onblur = function () {
    console.log("密码框失焦");
    let pwIptVal = pwIpt.value;
    let pwIptRegular = /^\S{3,18}$/;
    if (pwIptRegular.test(pwIptVal)) {
        this.nextElementSibling.innerHTML = '√';
        this.nextElementSibling.style.color = 'green';
        pwIptFlage = true;
    } else {
        this.nextElementSibling.innerHTML = '！ 请检查您输入的密码格式是否正确';
        this.nextElementSibling.style.color = 'red';
    }
};

// 登录点击
btn.onclick = async function () {
    let unIptVal = unIpt.value;
    let pwIptVal = pwIpt.value;
    console.log(unIptFlage);
    console.log(pwIptFlage);

    if (unIptFlage && pwIptFlage) {
        let url = 'http://jx.xuzhixiang.top/ap/api/login.php';
        let r = await axios.get(url, { params: { username: unIptVal, password: pwIptVal } });
        console.log(r);
        if (r.data.code == 1) {
            localStorage.setItem('uid', r.data.data.id);
            localStorage.setItem('token', r.data.data.token);
            localStorage.setItem('username', r.data.data.username);
            alert('登录成功');
            location.href = 'index.html';
        } else {
            alert(r.data.msg);
        }
    }
};
